package cc.yaomm.common.basic.excel.base;

import cc.yaomm.common.basic.base.SuperFunction;
import cc.yaomm.common.basic.util.ConvertUtils;
import com.alibaba.excel.metadata.Head;
import lombok.Getter;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * Created by XQL on 2021/3/14.
 */
@Getter
public abstract class SuperParamContext {

    /**
     * 操作人
     */
    protected Integer operatorId;

    /**
     * 操作人名称
     */
    protected String operatorName;

    /**
     * 导入excel文件
     */
    protected MultipartFile file;

    /**
     * 表头名称-列索引 的Map
     */
    protected Map<String, Head> headNameMap;

    /**
     * 属性字段-列索引 的Map
     */
    protected Map<String, Head> headPropMap;

    public void setHeadNameMap(Map<String, Head> headNameMap) {
        this.headNameMap = headNameMap;
    }

    public void setHeadPropMap(Map<String, Head> headPropMap) {
        this.headPropMap = headPropMap;
    }

    public <T> Integer getColumnIds(SuperFunction<T, ?> function) {
        Head head = headPropMap.get(ConvertUtils.toString(function));
        return Objects.isNull(head) ? 0 : head.getColumnIndex();
    }

    public <T> Set<Integer> getColumnIds(SuperFunction<T, ?>... function) {
        return Arrays.stream(function).map(x -> {
            Head head = headPropMap.get(ConvertUtils.toString(x));
            return Objects.isNull(head) ? 0 : head.getColumnIndex();
        }).collect(Collectors.toSet());
    }

}
